<!DOCTYPE html>

<html>
	<body onload="executeTests()">
	</body>
	<script>
		function executeTests() {
			var sampleString = "abcdefghijklmnopqrstuvwxyz";
			
			console.log("syntax");
			console.log("substr    >>> sampleString.substr(start, length);");
			console.log("substring >>> sampleString.substring(start, stop);");
			console.log("slice     >>> sampleString.slice(start, stop);");
			
			console.log("\nsample string being used >>> " + sampleString);
			console.log("length of sample string  >>> " + sampleString.length);
			
			console.log("\n--- Test Cases ---");
			
			console.log("1. first argument is 9 and second argument is 15");
			console.log("substr    >>> " + sampleString.substr(9, 15));
			console.log("substring >>> " + sampleString.substring(9, 15));
			console.log("slice     >>> " + sampleString.slice(9, 15));
			console.log("what happened:\nsubstr returned 15 chars from 9th char.\nsubstring and slice returned chars from 9th char to 14th char.");
			
			console.log("\n2. first argument is equal to second argument. using 20,20");
			console.log("substr    >>> " + sampleString.substr(20, 20));
			console.log("substring >>> " + sampleString.substring(20, 20));
			console.log("slice     >>> " + sampleString.slice(20, 20));
			console.log("what happened:\nas substr's second argument is length, if 20 chars are present from first argument then it is used otherwise the string length is used.");
			
			console.log("\n3. second argument is omitted. using 10 as the only argument.");
			console.log("substr    >>> " + sampleString.substr(10));
			console.log("substring >>> " + sampleString.substring(10));
			console.log("slice     >>> " + sampleString.slice(10));
			console.log("what happened:\nthe second argument is taken as length of the string.");

			console.log("\n4. first argument is less than 0 or is NaN, second argument is NaN. using '-1', 'xxx'");
			console.log("substr    >>> " + sampleString.substr(-1, 'xxx'));
			console.log("substring >>> " + sampleString.substring(-1, 'xxx'));
			console.log("slice     >>> " + sampleString.slice(-1, 'xxx'));
			console.log("what happened:\nboth the arguments are treated as zero.");

			console.log("\n5. first argument is NaN, second argument is less than 0. using 'xxx', -10");
			console.log("substr    >>> " + sampleString.substr('xxx', -10));
			console.log("substring >>> " + sampleString.substring('xxx', -10));
			console.log("slice     >>> " + sampleString.slice('xxx', -10));
			console.log("what happened:\ncontrary to test #4, if the second argument is negative then slice takes chars from the end of the string.\nso the second argument is equivalent to (length of string + second argument) in case of slice.\nwhere as both the arguments are treated as zero in case of substring and substr.");

			console.log("\n6. first argument is less than zero, second argument is a valid number. using -9, 9");
			console.log("substr    >>> " + sampleString.substr(-9, 9));
			console.log("substring >>> " + sampleString.substring(-9, 9));
			console.log("slice     >>> " + sampleString.slice(-9, 9));
			console.log("what happened:\nsubstr starts from the end of the string and replaces first argument with (string length + first argument) which would be 17.\nsubstring swaps both the arguments.\nslice does nothing.");
			
			console.log("\n7. either argument is greater than the length of the string. using 30,35");
			console.log("substr    >>> " + sampleString.substr(30, 35));
			console.log("substring >>> " + sampleString.substring(30, 35));
			console.log("slice     >>> " + sampleString.slice(30, 35));
			console.log("what happened:\neither argument will use the string's length");
			
			console.log("\n8. first argument is greater than second argument. using 15,9");
			console.log("substr    >>> " + sampleString.substr(15, 9));
			console.log("substring >>> " + sampleString.substring(15, 9));
			console.log("slice     >>> " + sampleString.slice(15, 9));
			console.log("what happened:\nsubstr returns 9 chars from 15th char.\nsubstring swaps both the arguments.\nslice does nothing.");
		}
	</script>
</html>
